package movieGandalf.dbParser;

import java.io.BufferedReader;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import movieGandalf.database.MovieTools;

public class LengthsAdder implements DataReader{
	
	public int read(Connection conn, BufferedReader in){
		String line = null;
		PreparedStatement stmt = null;
		
		BigInteger movieID = null;
		
		int len = 0;
		int start = -1;
		int end = -1;
		
		try {		
			
			stmt = conn.prepareStatement("CREATE TEMPORARY TABLE IF NOT EXISTS DbMysql06.TempLengths (movieID BIGINT UNSIGNED, length SMALLINT UNSIGNED, PRIMARY KEY (movieID));");
			stmt.execute();
			stmt.close();
			
			stmt = conn.prepareStatement("REPLACE INTO TempLengths (movieID,length) VALUES(?,?)");
			
			boolean isBegin = true;
			
			while ((line = in.readLine()) != null){
				if (isBegin){
					if ((line.length()<3 || !line.subSequence(0, 3).equals("===")))
						continue;
					else{
						isBegin = false;
						continue;
					}
				}
				
				int stringEnd = line.length();
				int i = 0;
				
				if (stringEnd>=5 && line.startsWith("-----"))
					break;
				
				if (stringEnd==0 || line.charAt(0)=='"')
					continue;
				
				for (i = line.length()-1;i>=0;i--){
					if (line.charAt(i)==')') {
						while (i>=0 && line.charAt(i)!='(')
							i--;
						i--;
						stringEnd=i;
						continue;
					}
					if (line.charAt(i)=='\t'){
						i++;
						break;
					}
				}
				
				if (i<0)
					continue;
				
				if (stringEnd-i<1)
					continue;
				
				String lengthString = line.substring(i, stringEnd);
				len = lengthString.length();
				start = -1;
				end = -1;
				for (i=0;i<len;i++){
					if (Character.isDigit(lengthString.charAt(i))){
						if (start == -1)
							start = i;
					}
					else{
						if (start != -1){
							end = i;
							break;
						}
					}
				}
				if (start == -1)
					continue;
				if (end==-1)
					end = i;
				
				lengthString = lengthString.substring(start,end);
				Integer length = Integer.parseInt(lengthString);
				
				String imdbName = ParserTools.getIMDBName(line);
				if (!MovieTools.movieValidator(imdbName))
					continue;
				
				movieID = MovieTools.getMovieID(conn, imdbName);
				if (movieID == null)
					continue;
				
				stmt.setObject(1, movieID);
				stmt.setObject(2, length);
				stmt.addBatch();
			}
			
			try{
				stmt.executeBatch();
			}
			catch(Exception e){}
			finally{
				conn.commit();
				stmt.close();
			}
			
			stmt = conn.prepareStatement("UPDATE Movies INNER JOIN TempLengths ON Movies.movieID=TempLengths.movieID SET Movies.length = TempLengths.length");
			stmt.addBatch();
			try{
				stmt.executeBatch();
			}
			catch(Exception e){}
			finally{
				conn.commit();
				stmt.close();
			}
		}
		
		catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
		catch (Exception e){
			e.printStackTrace();
		}
		finally {
			try{
				in.close();
			}
			catch(Exception e){
				return 0;
			}
		}
		
		return 1;
	}
	
}
